package com.xinsilu.hospital.controller;


import com.xinsilu.common.ResponseResult;
import com.xinsilu.hospital.entity.AuthsysRole;
import com.xinsilu.hospital.service.IAuthsysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IAuthsysRoleService roleService;
    @GetMapping("/search")
    public ResponseResult search(String roleName){
        ResponseResult responseResult = new ResponseResult();
        List<AuthsysRole> roleList = this.roleService.search(roleName);
        responseResult.setData(roleList);
        return responseResult;
    }

    @GetMapping("/search/{rlId}")
    public ResponseResult search(@PathVariable("rlId") int rlId){
        ResponseResult responseResult = new ResponseResult();
        AuthsysRole role = this.roleService.findById(rlId);
        if(role == null){
            responseResult.setStatus("not exist");
        }else{
            responseResult.setData(role);
        }
        return responseResult;
    }
    @PostMapping("/create")
    public ResponseResult add(@RequestBody AuthsysRole role){
        ResponseResult responseResult = new ResponseResult();
        this.roleService.add(role);
        responseResult.setData(role);
        return responseResult;
    }

    @PutMapping("/update/{rlId}")
    // 角色的基本信息， 分配角色的路由
    public ResponseResult update(@PathVariable("rlId") int rlId, @RequestBody AuthsysRole role){
        ResponseResult responseResult = new ResponseResult();
        this.roleService.update(rlId, role);
        return responseResult;
    }
    @PutMapping("/update/perms")
    // 给角色分配后台的权限
    public ResponseResult updatePermssion(@RequestBody AuthsysRole role){
        ResponseResult responseResult = new ResponseResult();
        this.roleService.updateRolePermissions(role);
        return responseResult;
    }

    @DeleteMapping("/delete/{rlId}")
    public ResponseResult delete(@PathVariable("rlId") int rlId){
        ResponseResult result = new ResponseResult();
        this.roleService.deleteRole(rlId);
        return result;
    }
}
